home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / afloat.zip / FTOL.ASM < prev    next >
Assembly Source File  |  1988-03-14  |  2KB  |  101 lines

  1.         PAGE ,132
  2. ;----------------------------------------------------------
  3. ; FTOL -- version for use with assembly language programs
  4. ;
  5. ; Copyright Bob Kline 1988
  6. ;
  7. ; Purpose:
  8. ;       Convert single-precision floating-point value
  9. ;    to four-byte long integer.
  10. ;
  11. ; Input:
  12. ;       DX:AX contain 4-byte real to be converted
  13. ;
  14. ; Output:
  15. ;    4-byte long integer returned in DX:AX.
  16. ;
  17. ; Other registers affected:
  18. ;       CX, BX
  19. ;
  20. ; Other procedures called:
  21. ;       none
  22. ;----------------------------------------------------------
  23.         PUBLIC  FTOL
  24.     EXTRN    _errno:WORD
  25.  
  26. ERANGE        EQU    34
  27.  
  28.     .MODEL    SMALL
  29.  
  30.     .CODE
  31.  
  32. FTOL    PROC
  33.  
  34. ; isolate sign and save it on the stack
  35.     MOV    CX,DX
  36.     AND    CX,8000h
  37.     PUSH    CX
  38.  
  39. ; unpack exponent and remove bias
  40.     MOV    BX,DX
  41.     AND    BX,7F80h
  42.     SHL    BX,1
  43.         XCHG    BH,BL
  44.     SUB    BX,127
  45.  
  46. ; if exponent is negative, result is zero
  47.     JNS    NON_ZERO
  48.     XOR    AX,AX
  49.     XOR    DX,DX
  50.     POP    CX
  51.     RET
  52.  
  53. ; if the exponent is greater than 30, the number
  54. ;   is too big for a 32-bit long integer
  55. NON_ZERO:
  56.     CMP    BX,31
  57.     JL    OK
  58.     MOV    _errno,ERANGE
  59.     POP    CX
  60.     RET
  61.  
  62. ; unpack mantissa
  63. OK:     AND     DX,7Fh
  64.         OR      DX,80h
  65.  
  66. ; if exponent is 23, we're done except for the sign
  67.         CMP     BX,23
  68.     JE    GET_SIGN
  69.  
  70. ; if the exponent is bigger than 23, we need to shift
  71. ;   left, otherwise, shift right
  72.     JG    BIG
  73.     MOV    CX,23
  74.     SUB    CX,BX
  75. LOOP1:    SHR    DX,1
  76.     RCR    AX,1
  77.     LOOP    LOOP1
  78.     JMP    SHORT GET_SIGN
  79.  
  80. ; large number -- shift mantissa to the left
  81. BIG:    MOV    CX,BX
  82.     SUB    CX,23
  83. LOOP2:    SHL    AX,1
  84.     RCL    DX,1
  85.     LOOP    LOOP2
  86.  
  87. ; get the sign back off the stack
  88. GET_SIGN:
  89.     POP    CX
  90.     OR    CX,CX
  91.     JZ    POSITIVE
  92.         NEG     DX
  93.         NEG     AX
  94.         SBB     DX,0
  95. POSITIVE:
  96.     RET
  97.  
  98. FTOL   ENDP
  99.  
  100.         END
  101.